package com.user.action;

import com.data.bean.Account;
import com.data.util.*;
import com.user.service.IndexUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户首页相关管理
 *
 * @author: HingLo
 * @create 2017-09-15 17:05
 **/
@Controller
@RequestMapping("/indexUserAction/user/")
public class IndexUserAction {
    @Autowired
    private IndexUserService indexUserService;
    @Autowired
    private AddAccountUtils addAccountUtils;

    @GetMapping("index")
    public String indexAction(HttpServletRequest request) {
        Account account = (Account) request.getSession().getAttribute("Account");
        if (account != null) {
            if (LoginMethodUtils.loginAuth(account.getTypes(), FinalStringUtils.PERSON, FinalStringUtils.COMPANY, FinalStringUtils.BUILT_ADMIN)) {
                ////查询自己发布的软件信息
                List<?> list = indexUserService.findPassSoftwareService(account.getId(), 0);
                request.setAttribute("dataMap", list);
                return "/WEB-INF/person/index";
            } else if (LoginMethodUtils.loginAuth(account.getTypes(), FinalStringUtils.ADMIN)) {
                return "/WEB-INF/admin/index";
            } else {
                return "index";
            }
        }
        return "login";
    }

    /**
     * 查看账号信息
     *
     * @return
     */

    @GetMapping("lookAccount")
    public String lookAccountMassageAction() {
        return "/WEB-INF/user/massage";
    }

    @GetMapping("check")
    @ResponseBody
    public String checkAccountStateAction(HttpServletRequest request) {
        Account account = (Account) request.getSession().getAttribute("Account");
        if (account != null && account.isIstate()) {
            return "true";
        } else {
            return "false";
        }
    }

    /**
     * 查看开发者信息
     *
     * @param request
     * @return
     */
    @GetMapping("lookDeveloper")
    @ResponseBody
    public List<?> lookDeveloperMassageAction(HttpServletRequest request) {
        Account account = (Account) request.getSession().getAttribute("Account");
        String id = account.getId();
        int type = account.getTypes();
        return indexUserService.lookDeveloperMassageService(id, type);
    }


    /**
     * 查看开发者的注册图片
     *
     * @param request
     * @param response
     */
    @GetMapping("developerIcon")
    public void developerIconAction(HttpServletRequest request, HttpServletResponse response) {
        Account account = (Account) request.getSession().getAttribute("Account");
        String id = account.getId();
        byte[] inputStream;
        inputStream = indexUserService.developerIconService(id);
        if (inputStream != null) {
            ImageLookUtils.responseImage(inputStream, response);
        }
    }


    /**
     * 修改密码
     *
     * @param request
     * @param password    密码吗
     * @param newPassword 新密码
     * @param oldPassword 旧密码
     * @return
     */
    @PostMapping("changePassword")
    @ResponseBody
    public String PasswordChangeAction(HttpServletRequest request, String password, String newPassword, String oldPassword) {
        if (CheckDataUtils.stringUtils(password, newPassword, oldPassword)) {
            boolean bool;
            if (password.equals(newPassword)) {
                oldPassword = Md5CodeUtils.md5(oldPassword);
                Account account = (Account) request.getSession().getAttribute("Account");
                if (oldPassword.equals(account.getPassword())) {
                    password = Md5CodeUtils.md5(password.trim());
                    account.setPassword(password);
                    bool = indexUserService.PasswordChangeService(account);
                    if (bool) {
                        request.getSession().removeAttribute("Account");
                        return "true";
                    } else {
                        return "密码修改失败，请稍后重试";
                    }
                }
                return "原密码错误";
            }
            return "两次输入的密码不一致";
        }
        return "输入的密码有误";
    }


    /***
     * 修改邮箱
     * @param email
     * @param id
     * @return
     */
    @PostMapping("changeEmail")
    public String changeEmailAction(String email, String id) {
        boolean bool;
        bool = indexUserService.changeEmailService(email, id);
        return String.valueOf(bool);
    }

    /***
     * 激活邮箱
     * @param code
     * @param email
     * @param request
     * @return
     */
    @GetMapping("activeEmailChange")
    public String activeEmailChangeAction(String code, String email, HttpServletRequest request) {
        boolean bool = false;
        if (CheckDataUtils.stringUtils(code, email)) {
            bool = indexUserService.activeEmailChangeService(code, email);
//系统自动登录
            if (bool) {
                addAccountUtils.addAccountSession(request, email);
            }
        }

        return bool ? "redirect:lookAccount" : "404";
    }


    public IndexUserService getIndexUserService() {
        return indexUserService;
    }

}
